package com.nf.db.handlers.map;

import java.sql.ResultSet;
import java.sql.SQLException;

import static com.nf.db.handlers.RowProcessor.DEFAULT_ROWPROCESSOR;

/*
 * 让ResultSetHandler处理结果集，并将结果集转换为map<Object,Object<<Bean>>
 * */
public class BeanMapHandler<T> extends AbstractMapHandler<T> {
    private String columnName;
    private Integer columnIndex;
    private Class<?> aClass;

    public BeanMapHandler(String columnName, Class<?> aClass) {
        this(columnName, -1, aClass);
    }

    /*
     * 这里没有无参构造器，是因为必须要传一个Class进来
     * 传一个Class进来，就可以根据Class的构造器来创建对象，然后将对象转换为map
     * */
    public BeanMapHandler(Integer columnIndex, Class<?> aClass) {
        this(null, columnIndex, aClass);
    }

    public BeanMapHandler(String columnName, Integer columnIndex, Class<?> aClass) {
        super(columnName,columnIndex);
        this.aClass = aClass;
    }

    @Override
    protected T getValue(ResultSet rs) throws SQLException {
        return (T) DEFAULT_ROWPROCESSOR.toBean(rs, aClass);
    }
}
